﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.Entity;
using ClassLibrary.DAO;
using System.IO;

public partial class Admin_ucControl_ucAddNewItem : System.Web.UI.UserControl
{
    ItemsDAO itemDAO = new ItemsDAO(Global.CONNECTION_STRING);
    TblCategoriesDAO cateDAO = new TblCategoriesDAO(Global.CONNECTION_STRING);
    TblItemImageDAO imageDAO = new TblItemImageDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (rdNo.Checked)
            {
                lbGiaKhuyenmai.Enabled = false;
                txtmakhuyenmai.Enabled = false;
                txtPricekhuyenmai.Enabled = false;
                lbmakhuyenmai.Enabled = false;
            }
            else
            {
                lbGiaKhuyenmai.Enabled = true;
                txtmakhuyenmai.Enabled = true;
                txtPricekhuyenmai.Enabled = true;
                lbmakhuyenmai.Enabled = true;
            }
            Load_Categories();
            string id = Request.QueryString["id"];
            if (id != null)
            {
                LoadDetail_Item();
                LoadImage_Item();
            }
        }
    }


    // Load Catelogories
    public void Load_Categories()
    {
        drLoaiSP.DataSource = cateDAO.GetAll();
        drLoaiSP.DataValueField = "CateID";
        drLoaiSP.DataTextField = "CateName";
        drLoaiSP.DataBind();
    }

    // Load Detail SP
    private void LoadDetail_Item()
    {
        Load_Categories();
        txtNumItem.Enabled = false;
        string id = Request.QueryString["id"];
        TblItems item = itemDAO.GetItemByID(Global.ConvertToInt(id));
        txtmakhuyenmai.Text = Convert.ToDouble(item.TaxPercentageRate).ToString();
        txtNumItem.Text = item.ItemNumber;
        txtPrice.Text = Convert.ToDouble(item.BaseSellingPrice).ToString();
        txtPricekhuyenmai.Text = Convert.ToDouble(item.PriceTaxCode).ToString();
        txtsoluong.Text = Convert.ToInt32(item.QuanlityOnHand).ToString();
        txtTenSP.Text = item.ItemName;
        if (item.TaxPercentageRate > 0)
        {
            rdYes.Checked = true;
            rdNo.Checked = false;
        }
        else
        {
            rdNo.Checked = true;
            rdYes.Checked = false;
        }
        drLoaiSP.SelectedValue = Convert.ToInt32(item.CateID).ToString();
        edttsp.Content = Server.HtmlDecode(item.ItemDescription);
    }

        // Load Image
        public void LoadImage_Item()
        {
            string id = Request.QueryString["id"];
            grdImgItems.DataSource = itemDAO.GetImgItems(Global.ConvertToInt(id));
            grdImgItems.DataBind();
        }

    protected void grdImgItems_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            foreach (DataControlFieldCell cell in e.Row.Cells)
            {
                // check all cells in one row
                foreach (Control control in cell.Controls)
                {
                    ImageButton button = control as ImageButton;
                    if (button != null && button.CommandName == "Delete")
                    {
                        // Add delete confirmation
                        button.CommandArgument = ((TblItemImage)e.Row.DataItem).ID.ToString();
                        button.OnClientClick = "if (!confirm('Are you sure to delete this Gallery?')) return false;";
                    }
                }
            }
        }
    }
    protected void btnDelImg_Click(object sender, ImageClickEventArgs e)
    {
        string id = Request.QueryString["id"];
        bool flag = false;
        try
        {
            TblItemImage imgItem = new TblItemImage();

            imgItem.ID = Convert.ToInt32(((ImageButton)sender).CommandArgument);
            int idXoa = Convert.ToInt32(((ImageButton)sender).CommandArgument);
             imgItem = imageDAO.GetItemImageByID(Global.ConvertToInt(id), idXoa);
             imageDAO.DeleteImgItem(imgItem);

            string filename = imgItem.ImageURL;
            if (File.Exists(Server.MapPath("~\\Admin\\img\\Items\\" + filename)))
            {
                File.Delete(Server.MapPath("~\\Admin\\img\\Items\\" + filename));
            }

            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
        if (flag)
        {
           // LoadImgItemByID();
        }
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        bool flag = false;
        try
        {

            string path = Server.MapPath("~\\Admin\\img\\Items\\");
            string filename = id + "_" + fileUpload.FileName;

            TblItemImage imgItem = new TblItemImage();
            // lblMessage.Text = path;
            if (System.IO.File.Exists(path + filename))
            {
                filename = DateTime.Now.ToString("mm") + "_" + DateTime.Now.ToString("ss") + "_" + filename;
            }
            else
            {
                filename = DateTime.Now.ToString("mm") + "_" + DateTime.Now.ToString("ss") + "_" + filename;
            }
            fileUpload.SaveAs(path + filename);
            imgItem.ImageURL = filename;
            imgItem.ItemID = Convert.ToInt32(id);

            itemDAO.InsImageItem(imgItem, 1);
            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
        if (flag)
        {         
                Response.Redirect("~/Admin/Danh-sach-san-pham.aspx");
        }
    }
 
    // thêm mới sản phẩm

    protected void btnLuu_Click(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        bool flag = false;
        try
        {
            TblItems item = new TblItems();
            item.IdLang = Convert.ToInt32 (drLang.SelectedValue);
            item.ItemDescription = Server.HtmlDecode(edttsp.Content);
            item.ItemName = txtTenSP.Text;
            item.ItemNumber = txtNumItem.Text;
            item.ItemType = 0;
            item.ValueOnHand = Global.ConvertToInt(txtsoluong.Text);
            item.BaseSellingPrice = Convert.ToDouble(txtPrice.Text);

            if (rdNo.Checked == true)
            {
                item.TaxPercentageRate = 0;
                item.PriceTaxCode = 0;
            }

            else
            {
                item.TaxPercentageRate = Convert.ToDouble(txtmakhuyenmai.Text);
                item.PriceTaxCode = Convert.ToDouble(txtPricekhuyenmai.Text);
            }
            item.CateID = Global.ConvertToInt(drLoaiSP.SelectedValue);
            item.QuanlityOnHand = Global.ConvertToInt(txtsoluong.Text);

            if (id == "" || id == null)
            {
                item.ItemID = 0;
                itemDAO.InsUp(item, 1);
            }
            else
            {
                item.ItemID = Global.ConvertToInt(id);
                itemDAO.InsUp(item, 2);
            }
            flag = true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        if (flag)
        {
            Response.Redirect("~/Admin/Danh-sach-san-pham.aspx");
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {

    }
    protected void btnDefault_Click(object sender, ImageClickEventArgs e)
    {

    }

    protected void txtmakhuyenmai_TextChanged(object sender, EventArgs e)
    {
        double khuyenmai = 0;
        khuyenmai = double.Parse(txtPrice.Text) * double.Parse(txtmakhuyenmai.Text)/100;
        txtPricekhuyenmai.Text = (double.Parse(txtPrice.Text) - khuyenmai).ToString();
    }
}
